package com.zwy.space.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.space.domain.Resource;
import com.zwy.space.queryVo.IndexVo;
import com.zwy.space.queryVo.ResourceTypeVo;
import com.zwy.space.queryVo.ResourceVo;
import com.zwy.space.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @RequestMapping
    public String showReource(Model model, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<ResourceVo> resourceList=resourceService.findAllResource();
        PageInfo<ResourceVo> pageInfo = new PageInfo<>(resourceList);
        List<ResourceTypeVo>types= resourceService.findAllTypes();
        model.addAttribute("types",types);
        model.addAttribute("pageInfo",pageInfo);
        return "resource";
    }


    @RequestMapping("/type/{typeId}")
    public String findResourceByType(@PathVariable int typeId ,@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,Model model){
        PageHelper.startPage(pageNum,10);
        List<ResourceVo> resourceList=resourceService.findResourceByType(typeId);
        PageInfo<ResourceVo> pageInfo = new PageInfo<>(resourceList);
        List<ResourceTypeVo>types= resourceService.findAllTypes();
        model.addAttribute("types",types);
        model.addAttribute("pageInfo",pageInfo);
       return "resource";
    }


}
